package com.zhuzg.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zhuzg.annotation.MesCache;
import com.zhuzg.mapper.StorageMapper;
import com.zhuzg.pojo.po.StorageInPo;
import com.zhuzg.pojo.po.StoragePO;
import com.zhuzg.pojo.request.StorageInItem;
import com.zhuzg.pojo.request.StorageInRequestDto;
import com.zhuzg.pojo.request.StorageRequestDto;
import com.zhuzg.pojo.response.StorageResponseDto;
import com.zhuzg.service.StorageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class StorageServiceImpl implements StorageService {

    @Autowired
    StorageMapper mapper;
    @Override
    public List<StorageResponseDto> query(StorageRequestDto condin) {

        QueryWrapper<StorageRequestDto> wrapper = new QueryWrapper<>();
        LambdaQueryWrapper<StorageRequestDto> eq = wrapper.lambda().eq(StorageRequestDto::getClass, "bb");


        // 数据查询出来是StoragePO类型
        List<StoragePO> poList  =  mapper.select(condin);
       // 需要转换成 StorageResponseDto 类型
        List<StorageResponseDto> dtoList = poList.stream().map(x -> {
            StorageResponseDto dto = new StorageResponseDto();
            return dto;
        }).collect(Collectors.toList());

        return dtoList;
    }

    /**
     * 一堆多的添加场景
     * @param storageInRequestDto
     */
    @Override
    public void add(StorageInRequestDto storageInRequestDto) {
        // 1. 添加主表
        StorageInPo storagePo = new StorageInPo(storageInRequestDto);
        mapper.insert(storagePo);

        //2. 添加子表 ---  获取明细
        List<StorageInItem> itemList = storageInRequestDto.getItemList();
        //明细数据 指定外键
        List<StorageInItem> inItems = itemList.stream(). // 集合转换成流
                 map(x -> {
            x.setInId(storagePo.getId()); // 设置外键id
            return x;
        })
                .collect(Collectors.toList());// stream 转换成集合

        mapper.insertItmes(inItems);

    }


    /**
     *  一堆多的修改的场景
     * @param storageInRequestDto
     */
    @Override
    public void update(StorageInRequestDto storageInRequestDto) {
        // 1. 修改主表
        StorageInPo storageInPo = new StorageInPo(storageInRequestDto);
        mapper.update(storageInPo);
        // 2. 根据外键进行  删除子表
        mapper.delteStorageInItemsByInId(storageInPo.getId());
        // 3. 重新插入子表 ---- 获取明细
        List<StorageInItem> itemList = storageInRequestDto.getItemList();
        // 3. 重新插入子表 ----   明细数据 指定外键
        List<StorageInItem> inItems = itemList.stream(). // 集合转换成流
                map(x -> {
            x.setInId(storageInPo.getId()); // 设置外键id
            return x;
        })
                .collect(Collectors.toList());// stream 转换成集合
        //  重新插入子表 ----   保存到数据库
        mapper.insertItmes(inItems);
    }

    /**
     * 一堆多的删除
     * @param idList
     */
    @Override
    public void delete(List<Integer> idList) {
        //  先删除主表
        mapper.deleteStorageIn(idList);
        // 删除子表
        mapper.delteStorageInItems(idList);

    }

    @Override
    public String testException() {
        throw new RuntimeException("测试异常");
       // return "ok";
    }

    @Override
    @MesCache(key="mescache",para="id",expiretime=3000)
    // 2  redis 当中是否有缓存  key=mescache:2 . 如果有则不执行storageService方法，直接从缓存当中获取数据
    //  如果没有  则执行findById方法，并且把数据放到redis 当中，有效期是3000
    public StorageResponseDto getById(Integer id) {
        return new StorageResponseDto(id);
    }

    @Override
    @MesCache(key="mescache",para="id",expiretime=3000)
    public StorageResponseDto get(String name, Integer id, String address) {
        return null;
    }

    @Override
    public Map getId(int id) {
        return mapper.getId(id);
    }
}
